package com.jshcbd.mvp_kt_demo.base.fragment

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.dylanc.viewbinding.base.inflateBindingWithGeneric

abstract class BaseSimpleFragment<VB : ViewBinding> : Fragment() {
    var binding: VB? = null
    private val _binding: VB get() = binding!!
    private var mRootView: View? = null
    var mActivity: Activity? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mActivity = activity
        if (mRootView == null) {
            binding = inflateBindingWithGeneric(layoutInflater, container, false)
            mRootView = _binding.root
            init()
        }
        val parent = mRootView!!.parent as ViewGroup
        parent.removeView(mRootView)
        return mRootView
    }

    protected abstract fun init()

    override fun onDestroyView() {
        super.onDestroyView()
        if (binding != null) {
            binding = null
        }
    }
}